import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

enum LabelType { text, qrcode, image }

enum LabelTextAlign { left, right, center }

class TemplateLabel {
  int id;
  double x, y, width, height;
  double offsetX = 0, offsetY = 0, offsetSize = 0;
  double? textSize;
  String? text;
  String? hintResId;
  double? textColor;
  String? fontFamily;
  LabelTextAlign? textAlign;
  double? drawTextHeight;
  LabelType labelType;

  /// 图片路径（支持网络、本地文件、asset）
  String? imageUrl;

  TemplateLabel({
    required this.x,
    required this.y,
    required this.width,
    required this.height,
    required this.labelType,
    this.textSize,
    this.id = 0,
    String? textContent,
    this.hintResId,
    this.textColor = 0x00000000,
    this.fontFamily,
    this.textAlign,
    this.imageUrl,
  }) : text = textContent ?? Intl.message("", name: hintResId);

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['x'] = x;
    data['y'] = y;
    data['offsetX'] = offsetX;
    data['offsetY'] = offsetY;
    data['offsetSize'] = offsetSize;
    data['width'] = width;
    data['height'] = height;
    data['textSize'] = textSize;
    data['text'] = text;
    data['hintResId'] = hintResId;
    data['textColor'] = textColor;
    data['textAlign'] = textAlign?.name;
    data['fontFamily'] = fontFamily;
    data['drawTextHeight'] = drawTextHeight;
    data['labelType'] = labelType.name;
    data['imageUrl'] = imageUrl;
    return data;
  }
}

class Template extends ChangeNotifier {
  int? id;
  String? _backgroundImage;
  double? _backgroundColor;
  double templateWidth;
  double templateHeight;
  final List<TemplateLabel> _labels;
  int checkLabelId;
  List<double>? enableColors;

  Template(
    this.templateWidth,
    this.templateHeight, {
    String? backgroundImage,
    double? backgroundColor,
    required List<TemplateLabel> labels,
    this.checkLabelId = -1,
    this.enableColors,
    this.id,
  }) : assert(
         backgroundImage == null || backgroundColor == null,
         "backgroundImage and backgroundColor can only be assigned one value",
       ),
       _backgroundImage = backgroundImage,
       _backgroundColor = backgroundColor,
       _labels = labels;

  String? get backgroundImage => _backgroundImage;

  double? get backgroundColor => _backgroundColor;

  List<TemplateLabel> get labels => _labels;

  set backgroundImage(String? value) {
    _backgroundImage = value;
    _backgroundColor = null;
    refreshUI();
  }

  set backgroundColor(double? value) {
    _backgroundColor = value;
    _backgroundImage = null;
    refreshUI();
  }

  void addLabel(TemplateLabel label) {
    _labels.add(label);
    refreshUI();
  }

  void refreshUI() {
    notifyListeners();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["id"] = id;
    data["backgroundImage"] = _backgroundImage;
    data["templateWidth"] = templateWidth;
    data["templateHeight"] = templateHeight;
    data["checkLabelIndex"] = checkLabelId;
    data["enableColors"] = enableColors;
    data["labels"] = _labels.map((e) => e.toJson()).toList();
    return data;
  }
}

enum DragAction { none, move, resize }
